import {getWithinAuth, postWithinAuth, getWithoutAuth} from '../../frame/ajax'
import {getUrl} from '../../frame/apiConfigMenber'

//合伙人判断
export const loadIsMenber = ({ dispatch }) => {
  return new Promise(
    (resolve, reject) => {
      getWithinAuth({
        url: getUrl('partner_is_bought')
      }).then(
        function (ret) {
          if (ret.message === 'NO_PARTNER') {
            dispatch('MENBER_IS', false)
          } else if (ret.message === 'OVER_TIME') {
            dispatch('MENBER_IS', true)
            dispatch('MENBER_IS_NOT_EXPIRED', false)
          } else if (ret.message === 'SUCCESS') {
            dispatch('MENBER_IS', true)
            dispatch('MENBER_IS_NOT_EXPIRED', true)
          }
          resolve()
        },
        function (err) {
          reject(err)
        }
      )
    }
  )
}

// 加载合伙人信息
export const loadMenberInfo = ({ dispatch }) => {
  return new Promise(
    (resolve, reject) => {
      getWithinAuth({
        url: getUrl('partner_load_info')
      }).then(
        function (ret) {
          // console.log('menberInfo', ret);
          dispatch('MENBER_INFO', ret);
          resolve()
        },
        function (err) {
          reject(err)
        }
      )
    }
  )
}

// 绑定合伙人上线id
export const partnerBinding = ({ dispatch }, parentId) => {
  return new Promise(
    (resolve, reject) => {
      postWithinAuth({
        url: getUrl('partner_bind_id'),
        data: parentId
      }).then(
        function (ret) {
          console.log('绑定成功', ret);
          // window.alert('上线绑定成功')
          resolve()
        },
        function (err) {
          console.log('没绑定成功');
          // window.alert('上线没绑定成功')
          reject(err)
        }
      )
    }
  )
}

// 绑定订单上线id
export const orderPartnerBinding = ({ dispatch }, {parentId, itemId, type}) => {
  return new Promise(
    (resolve, reject) => {
      postWithinAuth({
        url: getUrl('partner_order_bind'),
        data: {
          parentId,
          itemId,
          type
        }
      }).then(
        function (ret) {
          console.log('上线id绑定成功', ret);
          resolve()
        },
        function (err) {
          console.log('上线id没绑定成功');
          reject(err)
        }
      )
    }
  )
}

/**
 * 更新合伙人信息
 */
export const updateMenber = ({ dispatch }, isMenber) => {
  dispatch('MENBER_UPDATE', isMenber)
}

// 获取url参数
export const getUrlParam = ({ dispatch }, param) => {
  dispatch('MENBER_URL_PARAM', param);
}

/**
 * 素材库
 */
//加载邀请合伙人列表
export const loadInterviewList = ({ dispatch }) => {
  return new Promise(
    (resolve, reject) => {
      getWithoutAuth({
        url: getUrl('load_interview_list')
      }).then(
        function (interviewList) {
          dispatch('INTERVIEW_LOAD_INTERVIEW_LIST', interviewList)
          resolve()
        },
        function (err) {
          reject(err)
        }
      )
    }
  )
}
//加载邀请合伙人内容
export const loadInterviewRecord = ({ dispatch }, interviewId) => {
  return new Promise(
    (resolve, reject) => {
      getWithoutAuth({
        url: getUrl('load_interview_record').replace(':interviewId', interviewId)
      }).then(
        function (interviewRecordServer) {
          let paragraphArr = interviewRecordServer.paragraph
          const interviewRecord = paragraphArr.map(function (paragrap, index) {
            const subParagraphArr = paragrap.content.split('_#')
            let retObj = paragrap
            retObj.subParagraphArr = subParagraphArr
            delete retObj['content']
            return retObj
          })
          interviewRecordServer.paragraph = interviewRecord
          dispatch('INTERVIEW_LOAD_INTERVIEW_RECORD', interviewRecordServer)
          resolve()
        },
        function (err) {
          reject(err)
        }
      )
    }
  )
}

//加载课程推广列表
export const loadGoodsPromotionList = ({ dispatch }) => {
  return new Promise(
    (resolve, reject) => {
      getWithoutAuth({
        url: getUrl('load_goods_promotion_list')
      }).then(
        function (goodsPromotionList) {
          dispatch('LOAD_GOODS_PROMOTION_LIST', goodsPromotionList)
          resolve()
        },
        function (err) {
          reject(err)
        }
      )
    }
  )
}
//加载课程推广内容
export const loadGoodsPromotionRecord = ({ dispatch }, siId) => {
  return new Promise(
    (resolve, reject) => {
      getWithoutAuth({
        url: getUrl('load_goods_promotion_record').replace(':siId', siId)
      }).then(
        function (goodsPromotionRecordServer) {
          let paragraphArr = goodsPromotionRecordServer.paragraph
          const goodsPromotionRecord = paragraphArr.map(function (paragrap, index) {
            const subParagraphArr = paragrap.content.split('_#')
            let retObj = paragrap
            retObj.subParagraphArr = subParagraphArr
            delete retObj['content']
            return retObj
          })
          goodsPromotionRecordServer.paragraph = goodsPromotionRecord
          dispatch('LOAD_GOODS_PROMOTION_RECORD', goodsPromotionRecordServer)
          resolve()
        },
        function (err) {
          reject(err)
        }
      )
    }
  )
}
